package edu.cebanc.spring.biblioteca.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import edu.cebanc.spring.biblioteca.domain.BookCopy;

@Repository("bookCopiesDAO")
public class BookCopiesDAOimpl implements BookCopiesDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void saveBookCopy(BookCopy bookCopy) {
		sessionFactory.getCurrentSession().saveOrUpdate(bookCopy);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BookCopy> listBookCopies() {
		return (List<BookCopy>) sessionFactory.getCurrentSession().createCriteria(BookCopy.class)
										.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
	}

	@Override
	public BookCopy getBookCopy(int id) {
		return (BookCopy) sessionFactory.getCurrentSession().get(BookCopy.class, id);
	}

	@Override
	public void delBookCopy(BookCopy bookCopy) {
		sessionFactory.getCurrentSession().delete(bookCopy);
	}

	
}
